package com.yanqu.road.dao.impl.channelcenter.washe;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.washe.data.WasheHistory;
import com.yanqu.road.entity.activity.washe.data.WasheHistoryTemp;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WasheHistoryDaoImpl extends TempDataObjectDao<WasheHistory> {

    public static String getWasheHistorySql = "select * from `t_u_washe_history` where `userId`=?";

    public WasheHistory getWasheHistory(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getWasheHistorySql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateWasheHistorySql = "update `t_u_washe_history` set seasonHistoryTempMap=?,historyTempMap=?,lastGameHistoryTempMap=?,cardUseTimesMap=? where userId=?";

    @Override
    protected boolean updateData(WasheHistory washeHistory) {
        washeHistory.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeHistory.getSeasonHistoryTempMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeHistory.getHistoryTempMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeHistory.getLastGameHistoryTempMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeHistory.getCardUseTimesMap())));
        para.put(i++, new DbParameter(Types.BIGINT, washeHistory.getUserId()));
        if (getDb().executeNoneQuery(updateWasheHistorySql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertWasheHistorySql = "insert into `t_u_washe_history`(userId,seasonHistoryTempMap,historyTempMap,lastGameHistoryTempMap,cardUseTimesMap) values (?,?,?,?,?)";

    @Override
    protected boolean addData(WasheHistory washeHistory) {
        washeHistory.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, washeHistory.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeHistory.getSeasonHistoryTempMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeHistory.getHistoryTempMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeHistory.getLastGameHistoryTempMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeHistory.getCardUseTimesMap())));
        if (getDb().executeNoneQuery(insertWasheHistorySql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected WasheHistory parseData(ResultSet rs) throws SQLException {
        WasheHistory washeHistory = new WasheHistory();
        washeHistory.setUserId(rs.getLong("userId"));
        washeHistory.setSeasonHistoryTempMap(JSON.parseObject(rs.getString("seasonHistoryTempMap"), new TypeReference<ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, WasheHistoryTemp>>>() {
        }));
        washeHistory.setHistoryTempMap(JSON.parseObject(rs.getString("historyTempMap"), new TypeReference<ConcurrentHashMap<Integer, WasheHistoryTemp>>() {
        }));
        washeHistory.setLastGameHistoryTempMap(JSON.parseObject(rs.getString("lastGameHistoryTempMap"), new TypeReference<ConcurrentHashMap<Integer, WasheHistoryTemp>>() {
        }));
        washeHistory.setCardUseTimesMap(JSON.parseObject(rs.getString("cardUseTimesMap"), new TypeReference<ConcurrentHashMap<Integer, Long>>() {
        }));
        return washeHistory;
    }

}
